home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Burning & Media / GB-PVR 1.2.13 / GBPVR10213.msi / Cabs.w1.cab / Login.aspx.cs531 < prev    next >
Text File  |  2008-01-03  |  14KB  |  347 lines

  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Xml;
  8. using GBPVR.Public;
  9.  
  10. namespace gbweb
  11. {
  12.     /// <summary>
  13.     /// Summary description for Login.
  14.     /// </summary>
  15.     public partial class Login : Page
  16.     {
  17.         protected string salt;
  18.         private string requiredGuestPasswordHash;
  19.         private String requiredUsername;
  20.         private String requiredPasswordHash;
  21.         private string hostAddress = string.Empty;
  22.         protected Settings guideParams;
  23.     
  24.         protected void Page_Load(object sender, EventArgs e)
  25.         {
  26.             getTheme(); 
  27.             
  28.             Session["GuideStartTime"] = null;
  29.             Session["NotGuestUser"] = "true";
  30.             logo.Src = logo.Src.Replace("images", Convert.ToString(Session["theme"]));
  31.             
  32.             Logger.Info("Loading Config...");
  33.             XmlDocument configDoc = Global.Config;
  34.  
  35.             // extract username and password
  36.             requiredUsername = "admin";
  37.             requiredPasswordHash = string.Empty;
  38.             XmlNode node = configDoc.SelectSingleNode("/settings/WebUsername");
  39.             if (node != null)
  40.             {
  41.                 requiredUsername = node.InnerText;
  42.             }
  43.             node = configDoc.SelectSingleNode("/settings/WebPassword");
  44.             if (node != null)
  45.             {
  46.                 requiredPasswordHash = node.InnerText;
  47.             }
  48.             guideParams = Global.Settings;
  49.             requiredGuestPasswordHash = guideParams.GuestPassword;
  50.             Logger.Info(" ");
  51.             Logger.Info("Pulled Credential from Config...");
  52.             Logger.Info("User= " + requiredUsername);
  53.             Logger.Info("Password = " + requiredPasswordHash);
  54.             Logger.Info(" ");
  55.             if (!IsPostBack)
  56.             {
  57.                 Logger.Info("Postback not found....now pulling User Host Info...");
  58.                 if (Request.UserHostAddress.Substring(0, 1) == ":")
  59.                 {
  60.                     Logger.Info("IP V6 Detected on Server ");
  61.                     if (Global.Settings.autologinNets.CheckNumber(GetIP4Address()))
  62.                     {
  63.                         hostAddress = GetIP4Address();
  64.                         Logger.Info("Using IP4 Address: " + hostAddress);
  65.                         Logger.Info(" ");
  66.                         CompleteLogin();
  67.                     }
  68.                 }
  69.                 else
  70.                 {
  71.                     Logger.Info("IP V4 Detected on Server");
  72.                     if (Global.Settings.autologinNets.CheckNumber(Request.UserHostAddress))
  73.                     {
  74.                         hostAddress = Request.UserHostAddress;
  75.                         Logger.Info("Using IP4 Address: " + hostAddress);
  76.                         Logger.Info(" ");
  77.                         CompleteLogin();
  78.                     }
  79.                 }
  80.             }
  81.             Logger.Info(" ");
  82.             Logger.Info("Setting logo attribute to version info...");
  83.             logo.Attributes.Add("title", Global.Settings.GetVersionToolTip());
  84.             Logger.Info("Establishing login salt");
  85.             // allocate salt if we havn't already
  86.             if (Session["LoginSalt"] == null)
  87.             {
  88.                 Session["LoginSalt"] = Guid.NewGuid().ToString();
  89.             }
  90.             salt = (string)Session["LoginSalt"];
  91.             Logger.Info("Login salt = " + salt);
  92.             Logger.Info("Registering hidden field...LoginButton.UniqueID = " + LoginButton.UniqueID);
  93.             Logger.Info(" ");
  94.             ClientScript.RegisterHiddenField("__EVENTTARGET", LoginButton.UniqueID);
  95.  
  96.         }
  97.  
  98.         #region Web Form Designer generated code
  99.         override protected void OnInit(EventArgs e)
  100.         {
  101.             //
  102.             // CODEGEN: This call is required by the ASP.NET Web Form Designer.
  103.             //
  104.             InitializeComponent();
  105.             base.OnInit(e);
  106.         }
  107.         
  108.         /// <summary>
  109.         /// Required method for Designer support - do not modify
  110.         /// the contents of this method with the code editor.
  111.         /// </summary>
  112.         private void InitializeComponent()
  113.         {    
  114.             this.ID = "LoginButton";
  115.  
  116.         }
  117.         #endregion
  118.  
  119.         protected void LoginButton_Click(object sender, EventArgs e)
  120.         {
  121.             Logger.Info("Loginbutton click detected...");
  122.             // check credentials
  123.  
  124.  
  125.             // generate the expected hash for admin user and guest user
  126.             string expectedHash = FormsAuthentication.HashPasswordForStoringInConfigFile(requiredPasswordHash.ToLower() + salt, "MD5");
  127.             string GuestexpectedHash = FormsAuthentication.HashPasswordForStoringInConfigFile(requiredGuestPasswordHash.ToLower() + salt, "MD5");
  128.  
  129.  
  130.             // direct user to "manage listings" if credentials are correct
  131.             invalidCredentialsLabel.Visible = true;
  132.             Logger.Info("Now pulling User Host Info...");
  133.             if (Request.UserHostAddress.Substring(0, 1) == ":")
  134.             {
  135.                 Logger.Info("IP V6 Detected on Server ");
  136.                 hostAddress = GetIP4Address();
  137.                 Logger.Info("Using IP4 Address: " + hostAddress);
  138.             }
  139.             else
  140.             {
  141.                 hostAddress = Request.UserHostAddress;
  142.                 Logger.Info("Using IP4 Address: " + hostAddress);
  143.             }
  144.  
  145.             Logger.Info(" ");
  146.             Logger.Info("Loading Global Settings.... ");
  147.             Logger.Info(" ");
  148.             guideParams = Global.Settings;
  149.             Logger.Info(" ");
  150.             Session["NotGuestUser"] = "true";
  151.             Logger.Info("Checking to see if the username and password that were entered match what was pulled from config....");
  152.             if ((Username.Value == requiredUsername) &&
  153.                 (PasswordHash.Value == expectedHash.ToLower()))
  154.             {
  155.                 Logger.Info("User name and password matched for Administrative access...");
  156.                 Logger.Info(" ");
  157.                 CompleteLogin();
  158.             }
  159.             else
  160.             {
  161.                 // Create a StringComparer an comare the hashes.
  162.                 StringComparer comparer = StringComparer.OrdinalIgnoreCase;
  163.  
  164.                 if ((0 == comparer.Compare(PasswordHash.Value, GuestexpectedHash)) && Username.Value == guideParams.GuestUser)
  165.                 {
  166.                     Session["NotGuestUser"] = "false";
  167.                     Logger.Info("User name and password matched for Guest access...");
  168.                     Logger.Info(" ");
  169.                     CompleteLogin();
  170.                 }
  171.                 else
  172.                 {
  173.                     Logger.Warning("Failed login for " + Username.Value + " from " + hostAddress);
  174.                     Logger.Info("Pasword hash value = " + PasswordHash.Value);
  175.                     Logger.Info("Expected Admin hash value = " + expectedHash.ToLower());
  176.                     Logger.Info("Expected Guest hash value = " + guideParams.GuestPassword);
  177.                 }
  178.             }
  179.         }
  180.  
  181.         private void CompleteLogin()
  182.         {
  183.             Logger.Info("You have made it to the complete login method....this is a good thing.... ");
  184.  
  185.             string userName = Username.Value;
  186.             bool createPersistentCookie = false;
  187.             string cookiePath = FormsAuthentication.FormsCookiePath;
  188.             string redirectUrl = FormsAuthentication.GetRedirectUrl(userName, createPersistentCookie);
  189.             Uri redirectUri = new Uri(Request.Url, redirectUrl);
  190.  
  191.             Logger.Info("RedirectUri =  " + redirectUri.LocalPath);
  192.  
  193.             Logger.Info("Checking username not blank, redirect uri = admin/admin2.aspx... ");
  194.             if ((Username.Value.Length == 0) &&
  195.                 (((string.Compare(redirectUri.LocalPath, Request.ApplicationPath + "/Admin.aspx", true) == 0)) ||
  196.                 ((string.Compare(redirectUri.LocalPath, Request.ApplicationPath + "/Admin2.aspx", true) == 0))))
  197.             {
  198.                 Logger.Info("User name was blank and the redirect uri and request path were set to admin.aspx... ");
  199.                 Logger.Info(" ");
  200.                 return;
  201.             }
  202.  
  203.             if (string.Compare(redirectUri.LocalPath, Request.ApplicationPath + "/Admin2.aspx", true) == 0)
  204.             {
  205.                 Logger.Info("User is going to css based admin2.aspx...setting theme session variable ");
  206.                 Logger.Info(" ");
  207.                 getTheme();
  208.             }
  209.  
  210.             Logger.Info("Checking to see if the redirec uri is equal to default.aspx or logout.aspx ");
  211.             if ((string.Compare(redirectUri.LocalPath, Request.ApplicationPath + "/Default.aspx", true) == 0) ||
  212.                 (string.Compare(redirectUri.LocalPath, Request.ApplicationPath + "/Logout.aspx", true) == 0))
  213.             {
  214.                 Logger.Info(
  215.                     "Redirect uri was equal to default.aspx or logout.aspx....seting the redirect to guide.aspx...... ");
  216.                     Logger.Info(" ");
  217.                     redirectUrl = Request.ApplicationPath + "/Guide.aspx";
  218.  
  219.             }
  220.             else
  221.             {
  222.                 if ((string.Compare(redirectUri.LocalPath, Request.ApplicationPath + "/Default2.aspx", true) == 0) ||
  223.                     (string.Compare(redirectUri.LocalPath, Request.ApplicationPath + "/Logout2.aspx", true) == 0))
  224.                 {
  225.                     Logger.Info(
  226.                         "Redirect uri was equal to default2.aspx or logout2.aspx....seting the redirect to guide2.aspx...... ");
  227.                     Logger.Info(" ");
  228.                     getTheme();
  229.                     redirectUrl = Request.ApplicationPath + "/Guide2.aspx";
  230.                 }
  231.                 else
  232.                 {
  233.                     Logger.Info("Redirect uri was not equal to default or logout.... ");
  234.                     Logger.Info(" ");
  235.                 }
  236.             }
  237.  
  238.             Logger.Info(" ");
  239.             if (Username.Value.Length == 0)
  240.             {
  241.                 Logger.Info("Automatic login from " + hostAddress);
  242.                 Session["NotGuestUser"] = "true";
  243.                 Logger.Info(" ");
  244.  
  245.             }
  246.             else
  247.             {
  248.                 Logger.Info("Successful login for " + Username.Value + " from " + hostAddress);
  249.                 Logger.Info(" ");
  250.             }
  251.  
  252.             Logger.Info("Initializing Forms Authentication.... ");
  253.             Logger.Info(" ");
  254.             FormsAuthentication.Initialize();
  255.             Logger.Info("Setting Forms Authentication Cookie.... ");
  256.             Logger.Info("Username = " + Username.Value);
  257.             Logger.Info("Cookie Path = " + cookiePath);
  258.             Logger.Info(" ");
  259.             FormsAuthentication.SetAuthCookie(Username.Value, createPersistentCookie, cookiePath);
  260.  
  261.             Logger.Info("Checking for auto search execution.... ");
  262.             if ((guideParams.autoShowSearch || guideParams.autoShowRecord) && (String.Compare(guideParams.lastAutoSearchDate, DateTime.Now.ToShortDateString()) != 0))
  263.             {
  264.                 Logger.Info("Auto Search found and has not yet been executed today.... ");
  265.                 XmlNode EPGHour = Global.Config.SelectSingleNode("/settings/UpdateEPGHour");
  266.                 Logger.Info("Checking if it is at least one hour past the EPG update for today.... ");
  267.                 if (DateTime.Now.Hour + 1 > Convert.ToInt32(EPGHour.InnerText))
  268.                 {
  269.                     Logger.Info("It is greater than one hour since todays EPG update.... ");
  270.                     Logger.Info("Executing auto search.... ");
  271.                     SavedSearchUtils searchUtil = new SavedSearchUtils();
  272.                     searchUtil.autoSearch(searchUtil.loadAutoSearches());
  273.                     Logger.Info("Setting last auto search date to today so this routine does not run again until tommorow.... ");
  274.                     guideParams.lastAutoSearchDate = DateTime.Now.ToShortDateString();
  275.                     guideParams.Save();
  276.                 }
  277.                 else
  278.                 {
  279.                     Logger.Info("Auto search can not execute now since it is not 1 hour greater than todays EPG update.... ");
  280.                 }
  281.             }
  282.             else
  283.             {
  284.                 Logger.Info("Auto Search is not set to run or it has aleady run today.... ");
  285.             }
  286.             Logger.Info(" ");
  287.             Logger.Info("Redirecting to the target page.... ");
  288.             Logger.Info("Redirect URL = " + redirectUrl);
  289.             Response.Redirect(redirectUrl, true);
  290.             Logger.Info(" ");
  291.         }
  292.  
  293.         public string GetIP4Address()
  294.         {
  295.             string strIP4Address = String.Empty;
  296.  
  297.             foreach (IPAddress objIP in Dns.GetHostAddresses(Dns.GetHostName()))
  298.             {
  299.                 if (objIP.AddressFamily.ToString() == "InterNetwork")
  300.                 {
  301.                     strIP4Address = objIP.ToString();
  302.                     break;
  303.                 }
  304.             }
  305.             return strIP4Address;
  306.         }
  307.  
  308.         private void getTheme()
  309.         {
  310.             //Check to see if the theme has been set in session and that the set theme is using the table view
  311.             string theme = Convert.ToString(Session["theme"]);
  312.  
  313.             if (theme != null && theme != "" && theme.Substring(0, 7) == "themes/")
  314.             {
  315.                 return;
  316.             }
  317.             else
  318.             {
  319.                 //Since the session theme variable was not set or is using the css view we need to read the value from the cookie
  320.                 HttpCookie cookie = Request.Cookies["theme"];
  321.                 if (cookie != null && cookie.Value.Length > 0)
  322.                 {
  323.                     theme = cookie.Value;
  324.                 }
  325.                 else
  326.                 {
  327.                     theme = "Default";
  328.                 }
  329.  
  330.                 //Verify that that the theme in the cookie is available in the table based selections.  If it is not found set the theme to Default.
  331.                 if (File.Exists(HttpContext.Current.Server.MapPath("~/themes/") + theme + "/styles.css"))
  332.                 {
  333.                     Session["theme"] = "themes/" + theme;
  334.                 }
  335.                 else
  336.                 {
  337.                     Session["theme"] = "themes/Default";
  338.                 }
  339.  
  340.  
  341.  
  342.                 return;
  343.             }
  344.         }
  345.     }
  346. }
  347.